using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Character : MonoBehaviour
{
   // public Transform _hookPrefab;
    public Transform _currentPlanet;
    public float _speed = 6.0f;
    public float _damping = 0.5f;
    public float _intensity = 3.0f;
    public float _jumpForce = 5.0f;

    private float _zScale = 1.0f;
    private Vector3 _gravitationalPull = Vector3.zero;
    private Vector3 _moveDirection = Vector3.zero;
    private Vector3 _currentNormal;

    private RaycastHit _raycastHitOnGround;
    private RaycastHit _raycastHitWhileFalling;
    private Vector3 _objToPlanetDirection;

    void Start()
    {
        _currentNormal = transform.up;
        _zScale = transform.localScale.z;
        gameObject.AddComponent("Rigidbody");
        gameObject.rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
        gameObject.rigidbody.useGravity = false;
    }

    void Update()
    {
        Quaternion quaternionRotation;
        if (!gameObject.rigidbody.isKinematic)
        {
            
            Vector3 objToPlanetDirection = (_currentPlanet.transform.position - gameObject.transform.position).normalized;
            _currentNormal = Vector3.Lerp(_currentNormal, _raycastHitWhileFalling.normal, _damping);
            quaternionRotation = Quaternion.FromToRotation(transform.up, _currentNormal);
            transform.rotation = quaternionRotation * transform.rotation;
        }
        else
        {
            /*
            _moveDirection = new Vector3(0, 0, Input.GetAxis("Horizontal"));
            _moveDirection = transform.TransformDirection(_moveDirection);
            _moveDirection *= _speed;

            _currentNormal = Vector3.Lerp(_currentNormal, _raycastHitOnGround.normal, _damping);

            quaternionRotation = Quaternion.FromToRotation(transform.up, _currentNormal);
            transform.rotation = quaternionRotation * transform.rotation;

            gameObject.transform.position += _moveDirection * Time.deltaTime;
             * */
        }
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Quaternion quaternionRotation;
        if (!gameObject.rigidbody.isKinematic)
        {
            Vector3 _objToPlanet = _currentPlanet.position - gameObject.transform.position;
            Vector3 _gravity = (_objToPlanet.normalized / (_objToPlanet.magnitude * _objToPlanet.magnitude)) * _currentPlanet.rigidbody.mass * gameObject.rigidbody.mass;
            gameObject.rigidbody.AddForce(_gravity * _intensity);

            _objToPlanetDirection = (_currentPlanet.transform.position - gameObject.transform.position).normalized;
            Physics.Raycast(transform.position, _objToPlanetDirection, out _raycastHitWhileFalling, 100.0f);


        }
        else
        {
            _objToPlanetDirection = (_currentPlanet.transform.position - gameObject.transform.position).normalized;
            Physics.Raycast(transform.position, _objToPlanetDirection, out _raycastHitOnGround, 10.0f);
           // print(_raycastHitOnGround.distance);

            _moveDirection = new Vector3(0, 0, Input.GetAxis("Horizontal"));
            _moveDirection = transform.TransformDirection(_moveDirection);
            _moveDirection *= _speed;

            _currentNormal = Vector3.Lerp(_currentNormal, new Vector3(_raycastHitOnGround.normal.x, _raycastHitOnGround.normal.y, 0.0f), _damping);

            quaternionRotation = Quaternion.FromToRotation(transform.up, _currentNormal);
            //quaternionRotation.SetAxisAngle(transform.worldToLocalMatrix * transform.up, 0.0f);
            transform.rotation = quaternionRotation * transform.rotation;
            transform.rotation.SetAxisAngle(transform.up, 0.0f);

            _objToPlanetDirection *= -1.0f;
            gameObject.transform.position = _raycastHitOnGround.point + _objToPlanetDirection;

            gameObject.transform.position += new Vector3(_moveDirection.x, _moveDirection.y, 0.0f) * Time.deltaTime;

            if ( Input.GetButtonDown("Jump") )
            {
                print("Jump!!!");
                gameObject.rigidbody.isKinematic = false;
                gameObject.rigidbody.AddForce(gameObject.transform.up.x * _jumpForce, gameObject.transform.up.y * _jumpForce, 0.0f, ForceMode.Impulse);
            }
        }

        /*
        CharacterController characterController = (CharacterController)GetComponent("CharacterController");

        // Vector3 targetDir = transform.TransformDirection(-Vector3.up);
        Vector3 objToPlanetDirection = (_currentPlanet.transform.position - transform.position).normalized;

        RaycastHit raycastHit;
        RaycastHit raycastHitWhileFalling;
        Quaternion quaternionRotation;

        Physics.Raycast(transform.position, objToPlanetDirection, out raycastHitWhileFalling, 100.0f);

        if (!Physics.Raycast(transform.position, objToPlanetDirection, out raycastHit, 1.5f))
        {
            //_gravitationalPull += (transform.position - _planetCore.position).normalized * -2 * Time.deltaTime;
            _gravitationalPull = (objToPlanetDirection / (objToPlanetDirection.magnitude * objToPlanetDirection.magnitude)) * _currentPlanet.rigidbody.mass * 1.0f;
            characterController.Move(_gravitationalPull * 5.0f * Time.deltaTime);

            _currentNormal = Vector3.Lerp(_currentNormal, raycastHitWhileFalling.normal, _damping);
            quaternionRotation = Quaternion.FromToRotation(transform.up, _currentNormal);
            transform.rotation = quaternionRotation * transform.rotation;
        }
        else
        {
            // _gravitationalPull = Vector3.zero;
        }

        _gravitationalPull = (objToPlanetDirection / (objToPlanetDirection.magnitude * objToPlanetDirection.magnitude)) * _currentPlanet.rigidbody.mass;

        _moveDirection = new Vector3(0, 0, Input.GetAxis("Horizontal"));
        _moveDirection = transform.TransformDirection(_moveDirection);
        _moveDirection *= _speed;

        float inputDirection = Input.GetAxis("Horizontal");

        if (inputDirection > 0.0f)
        {
            transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, _zScale);
        }
        else if (inputDirection < 0.0f)
        {
            transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, -_zScale);
        }


        // float s = 0.3f;
        _currentNormal = Vector3.Lerp(_currentNormal, raycastHit.normal, _damping);

        quaternionRotation = Quaternion.FromToRotation(transform.up, _currentNormal);
        // quaternionRotation.x = 0.0f;
        transform.rotation = quaternionRotation * transform.rotation;
        //transform.Rotate(_currentNormal);

        if (Input.GetButton("Jump"))
        {
            _moveDirection.y += transform.up.y * 30.0f;
            _moveDirection.x += transform.up.x * 30.0f;
        }

        characterController.Move(_moveDirection * Time.deltaTime);
         * */
    }

    void OnCollisionEnter(Collision collision)
    {
        gameObject.rigidbody.isKinematic = true;
        print("Collision Enter");
    }

    void OnCollisionStay(Collision collision)
    {
        print("Collision Stay");
    }

    void OnCollisionExit(Collision collision)
    {
        print("Collision Exit");
    }
}
